import 'dart:io';

import 'package:geolocator/geolocator.dart';
import 'package:love_newf/app/com/base/app.dart';
import 'package:love_newf/app/com/view/common_view.dart';
import 'package:love_newf/generated/l10n.dart';
import 'package:permission_handler/permission_handler.dart';

class PermissionUtils {
  ///开播权限
  static Future<bool> checkPermission({goToSetting = true}) async {
    var b = await locationPermission(goToSetting: goToSetting);
    if (Platform.isAndroid) {
      if (b) b = await PermissionUtils.checkSMSPermission();
      if (b) b = await PermissionUtils.checkPhonePermission();
      if (b) b = await PermissionUtils.checkCalendarPermission();
    }
    return b;
  }

  ///定位权限
  static Future<bool> locationPermission({goToSetting = true}) async {
    LocationPermission permission;
    // Test if location services are enabled.
    var serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      // return Future.error('Location services are disabled.');
      //    return await Location().requestService();
      // DeviceDataPlugin.openLocationSetting;
      return true;
    }
    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        //位置权限被拒绝
        // return Future.error('Location permissions are denied');
        return false;
      }
    }
    if (permission == LocationPermission.deniedForever) {
      permission = await Geolocator.requestPermission();
      // if (App.localeEn) {
      //   //印度不弹窗
      //   isPermissionsDialog = true;
      // }
      if (!isPermissionsDialog && goToSetting) {
        showPermissionsDialog(
            title: App.localeEn
                ? "Inaccessible location"
                : "Ubicación inaccesible",
            message: App.localeEn
                ? "Allow the application to access the location"
                : "Permitir que la aplicación acceda a la ubicación");
      } else {
        return true;
      }
      return false;
    }
    return true;
  }

  static Future<bool> checkCameraPermission() async {
    //获取当前的权限
    var status = await Permission.camera.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.camera.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        showPermissionsDialog(
            title: "相机权限说明", message: "开播必须打开相机权限和麦克风权限，否则无法正常开播");
        return false;
      }
    }
  }

  static Future<bool> checkMicrophonePermission({message}) async {
    //获取当前的权限
    var status = await Permission.microphone.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.microphone.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        showPermissionsDialog(
            title: "麦克风权限使用说明", message: message??"用于录制语音、语音输入、拍摄视频的场景");
        return false;
      }
    }
  }

  Future<bool> requestCalendarPermission() async {
    //获取当前的权限
    var status = await Permission.phone.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.phone.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  static Future<bool> checkSMSPermission() async {
    //获取当前的权限
    var status = await Permission.sms.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.sms.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        showPermissionsDialog(
            title: S.current.smsPermissionTitle,
            message: S.current.smsaPermissionMessage);
        return false;
      }
    }
  }

  static Future<bool> checkCalendarPermission() async {
    //获取当前的权限
    var status = await Permission.calendar.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.calendar.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  static Future<bool> checkWifiPermission() async {
    //获取当前的权限
    var status = await Permission.nearbyWifiDevices.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.sms.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

  static Future<bool> checkContactPermission() async {
    // return true;//印度 不传
    //获取当前的权限
    var status = await Permission.contacts.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.contacts.request();

      ///上面没有发起请求。
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        // return true;
        showPermissionsDialog(
            title: S.current.contactPermissionTitle,
            message: S.current.contactPermissionMessage);
        return false;
      }
    }
  }

  static Future<bool> checkPhonePermission() async {
    //获取当前的权限
    var status = await Permission.phone.status;
    if (status == PermissionStatus.granted) {
//已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.phone.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        openAppSettings();
        return false;
      }
    }
  }
}
